package com.seeyon.ctp.rest.resources;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.seeyon.apps.reimburseplugin.listener.ReimburseListener;
import com.seeyon.apps.reimburseplugin.rest.ClientResource;
import com.seeyon.apps.reimburseplugin.util.YRUtil;
import com.seeyon.client.CTPRestClient;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgEntity;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.organization.manager.OrgManagerDirect;
import com.seeyon.ctp.util.DateUtil;
import com.seeyon.ctp.util.Strings;
import com.seeyon.ctp.util.json.JSONUtil;

@Path("fyflow")
@Consumes({"application/json", "application/xml"})
@Produces({"application/json"})
public class FyFlowReoucrce extends BaseResource{
	
	private static final Log logger=LogFactory.getLog(ReimburseListener.class);
	
	private OrgManager orgManager =  (OrgManager) AppContext.getBean("orgManager");

	private OrgManagerDirect orgManagerDirect = (OrgManagerDirect) AppContext.getBean("orgManagerDirect");
	
	@POST
	@Path("process/start")
	public  Response yrstartProcess(Map<String, Object> params) throws Exception {
		Map<String,Object> res = new HashMap<>();
		logger.info("yrstartProcess  params"+JSONUtil.toJSONString(params));
		String sendUser = YRUtil.getString(params.get("startUser"));
		String loginName = "";
		if(Strings.isNotEmpty(sendUser)) {
			V3xOrgMember orgMember = orgManager.getMemberByLoginName(sendUser);
			if(orgMember != null) {
				loginName = orgMember.getLoginName();
			}else {
				res.put("code", 100021);
				res.put("message", "发起者【"+sendUser+"】OA中不存在此用户！");
				return ok(res);
			}
		}else {
			res.put("code", 100020);
			res.put("message", "发起者不能为空");
			return ok(res);
		}
		Map<String, Object> formdata = (Map<String, Object>) params.get("formdata");
		V3xOrgEntity departentity = getAllEntityByCode(V3xOrgDepartment.class, YRUtil.getString(formdata.get("pk_reqdept_v")));//需求部门
		Long departId = -1l;
		if(departentity != null) {
			departId = departentity.getId();
		}else {
			res.put("code", 100010);
			res.put("message", "需求部门:【"+YRUtil.getString(formdata.get("pk_reqdept_v"))+"】OA中不存在此部门！");
			return ok(res);
		}
		Map<String, Object> map = new HashMap<String, Object>();
        map.put("appName", "collaboration");
        Map<String, Object> data1 = new HashMap<String, Object>();
      
        data1.put("draft", "0");
        
        String ctrantypeid = YRUtil.getString(formdata.get("ctrantypeid"));//申请类型
        if(ctrantypeid.equals("20-Cxx-01")) {
        	 data1.put("templateCode", "fy_specialized_asset");
        	 Map<String, Object> data2 = new HashMap<String, Object>();
        	 Map<String, Object> formmain_0139 = new HashMap<String, Object>();
             formmain_0139.put("申请单位", departId); //选部门
             formmain_0139.put("资产名称1", YRUtil.getString(formdata.get("name")));     //资产名称
             formmain_0139.put("申请科室", departId); //选部门 
             formmain_0139.put("资产名称", YRUtil.getString(formdata.get("name")));  //资产名称
             formmain_0139.put("申请配置资产数量", YRUtil.getString(formdata.get("nastnum"))); 
             formmain_0139.put("申请配置预算单价", YRUtil.getString(formdata.get("ntaxprice"))); 
             formmain_0139.put("同类资产现有存量", YRUtil.getString(formdata.get("vdef1")));//同类资产现有存量 
             formmain_0139.put("未满使用年限数", YRUtil.getString(formdata.get("vdef2")));//未满使用年限数 
             formmain_0139.put("预算执行剩余计划", YRUtil.getString(formdata.get("vdef3")));//预算执行剩余计划 
             formmain_0139.put("资产配置更新理由及相关依据", YRUtil.getString(formdata.get("vdef4")));//资产配置理由及更新依据
             formmain_0139.put("资产主要技术规格参数", YRUtil.getString(formdata.get("vdef5")));//资产主要技规格、参数
             formmain_0139.put("资产使用方案及预期使用年限", YRUtil.getString(formdata.get("vdef6")));//资产使用方案及预期使用年限
             formmain_0139.put("资产配置解决的问题及其产生的经济社会效益", YRUtil.getString(formdata.get("vdef7")));//资产配置解决的问题及其产生的经济社会效益
             formmain_0139.put("选型分析及选型理由", YRUtil.getString(formdata.get("vdef17")));//选型分析及理由
             formmain_0139.put("品牌型号1", YRUtil.getString(formdata.get("vdef8"))); //资产选型1品牌型号
             formmain_0139.put("品牌型号2", YRUtil.getString(formdata.get("vdef11")));//资产选型2品牌型号
             formmain_0139.put("品牌型号3", YRUtil.getString(formdata.get("vdef14")));//资产选型3品牌型号
             formmain_0139.put("主要技术规格参数1", YRUtil.getString(formdata.get("vdef9")));//资产选型1主要技术规格参数
             formmain_0139.put("主要技术规格参数2", YRUtil.getString(formdata.get("vdef12")));//资产选型2主要技术规格参数
             formmain_0139.put("主要技术规格参数3", YRUtil.getString(formdata.get("vdef15")));//资产选型3主要技术规格参数
             formmain_0139.put("单价1", YRUtil.getString(formdata.get("vdef10")));//资产选型1单价
             formmain_0139.put("单价2", YRUtil.getString(formdata.get("vdef13")));//资产选型2单价
             formmain_0139.put("单价3", YRUtil.getString(formdata.get("vdef16")));//资产选型3单价
             formmain_0139.put("申请单号", YRUtil.getString(formdata.get("vbillcode")));//申请单号
             formmain_0139.put("申请日期", YRUtil.getString(formdata.get("dbilldate")));//申请日期
             formmain_0139.put("预算项目", YRUtil.getString(formdata.get("vbdef1")));//预算项目
             formmain_0139.put("库存组织", YRUtil.getString(formdata.get("pk_org")));//库存组织
             formmain_0139.put("需求部门", YRUtil.getString(formdata.get("pk_reqdept_v")));//需求部门
     		
     		YRUtil.getString(formdata.get("fbillstatus"));//单据状态  (0=自由，1=提交，2=正在审批，3=审批通过，4=审批未通过，5=关闭) 取已审批的
     		//formmain_0139 测试     formmain_0171正式
             data2.put("formmain_0171", formmain_0139);
             data1.put("data", data2);
             map.put("data", data1);
        }else if(ctrantypeid.equals("20-Cxx-02")) {
        	 data1.put("templateCode", "fy_xm_ys");
        	 Map<String, Object> data2 = new HashMap<String, Object>();
             Map<String, Object> formmain_0139 = new HashMap<String, Object>();
             formmain_0139.put("表2申请处室", departId); //选部门
             formmain_0139.put("表2填报时间", DateUtil.getDate("yyyy-MM-dd"));     
             formmain_0139.put("表2支出项目", YRUtil.getString(formdata.get("vbdef2"))); 
             formmain_0139.put("表2支出事由及依据", YRUtil.getString(formdata.get("vbdef3")));  
             formmain_0139.put("表2预算金额及测算依据", YRUtil.getString(formdata.get("vbdef4"))); 
             data2.put("formmain_0178", formmain_0139);
             data1.put("data", data2);
             map.put("data", data1);
        }
        logger.info("yrstartProcess  map"+JSONUtil.toJSONString(map));
        CTPRestClient client = ClientResource.getInstance().resouresClent(loginName);
        String result = client.post("bpm/process/start" ,map, String.class);
		return ok(JSONUtil.parseJSONString(result, Map.class));
	}

	
	
	public V3xOrgEntity getAllEntityByCode(Class<? extends V3xOrgEntity> clazz,String value) {
		List<V3xOrgEntity> listEntity = this.getEntityNoRelationDirect(clazz, "code", value);
		if (listEntity.size() > 0) {
			Iterator var4 = listEntity.iterator();

			while (var4.hasNext()) {
				V3xOrgEntity ent = (V3xOrgEntity) var4.next();
				if (value.equals(ent.getCode())) {
					return ent;
				}
			}
		}
		return null;
	}
	
	public List<V3xOrgEntity> getEntityNoRelationDirect(Class<? extends V3xOrgEntity> clazz, String fieldName,
			String fieldValue) {
		List<V3xOrgEntity> listEntity = orgManagerDirect.getEntityNoRelationDirect(clazz.getSimpleName(),
				fieldName, fieldValue, (Boolean) null, (Long) null);
		return (List) (listEntity.size() > 0 ? listEntity : new ArrayList());
	}
	
	
	@POST
	@Path("process/xmys")
	public  Response yrstartProcessys(Map<String, Object> params) throws Exception {
		Map<String,Object> res = new HashMap<>();
		logger.info("yrstartProcessys  params"+JSONUtil.toJSONString(params));
		String sendUser = YRUtil.getString(params.get("startUser"));
		String loginName = "";
		if(Strings.isNotEmpty(sendUser)) {
			V3xOrgMember orgMember = orgManager.getMemberByLoginName(sendUser);
			if(orgMember != null) {
				loginName = orgMember.getLoginName();
			}else {
				res.put("code", 100021);
				res.put("message", "发起者【"+sendUser+"】OA中不存在此用户！");
				return ok(res);
			}
		}else {
			res.put("code", 100020);
			res.put("message", "发起者不能为空");
			return ok(res);
		}
		Map<String, Object> formdata = (Map<String, Object>) params.get("formdata");
		V3xOrgEntity departentity = getAllEntityByCode(V3xOrgDepartment.class, YRUtil.getString(formdata.get("pk_reqdept_v")));//需求部门
		Long departId = -1l;
		if(departentity != null) {
			departId = departentity.getId();
		}else {
			res.put("code", 100010);
			res.put("message", "需求部门:【"+YRUtil.getString(formdata.get("pk_reqdept_v"))+"】OA中不存在此部门！");
			return ok(res);
		}
		Map<String, Object> map = new HashMap<String, Object>();
        map.put("appName", "collaboration");
        Map<String, Object> data1 = new HashMap<String, Object>();
        data1.put("templateCode", "fy_xm_ys");
        data1.put("draft", "0");
        Map<String, Object> data2 = new HashMap<String, Object>();
        Map<String, Object> formmain_0139 = new HashMap<String, Object>();
        formmain_0139.put("申请处室", departId); //选部门
        formmain_0139.put("填报时间", DateUtil.getDate("yyyy-MM-dd"));     
        formmain_0139.put("支出项目", YRUtil.getString(formdata.get("vbdef2"))); 
        formmain_0139.put("支出事由及依据", YRUtil.getString(formdata.get("vbdef3")));  
        formmain_0139.put("预算金额及测算依据", YRUtil.getString(formdata.get("vbdef4"))); 
        data2.put("formmain_0178", formmain_0139);
        data1.put("data", data2);
        map.put("data", data1);
        CTPRestClient client = ClientResource.getInstance().resouresClent(loginName);
        String result = client.post("bpm/process/start" ,map, String.class);
		return ok(JSONUtil.parseJSONString(result, Map.class));
	}
}
